package com.ebid.templates.ten;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QualifyBulletinPhase {

    public static String qualifyBulletinPhase ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            +"<qualifyBulletinPhase> \n" +
            "    <TENDER_PROJECT_CODE>{0}</TENDER_PROJECT_CODE>\n" +
            "    <BID_SECTION_CODES>" +
            "      {1}" +
            "    \n\t</BID_SECTION_CODES>\n" +
            "    <TENDER_CODE>{2}</TENDER_CODE>\n" +
            "    <NOTICE_NAME>{3}</NOTICE_NAME>\n" +
            "    <NOTICE_CONTENT>{4}</NOTICE_CONTENT>\n" +
            "    <NOTICE_SEND_TIME>{5}</NOTICE_SEND_TIME>\n" +
            "    <NOTICE_MEDIA>{6}</NOTICE_MEDIA>\n" +
            "    <URL>{7}</URL>\n" +
            "    <NOTICE_NATURE>{8}</NOTICE_NATURE>\n" +
            "    <NOTICE_TYPE>{9}</NOTICE_TYPE>\n" +
            "    <DOC_GET_START_TIME>{10}</DOC_GET_START_TIME>\n" +
            "    <DOC_GET_END_TIME>{11}</DOC_GET_END_TIME>\n" +
            "    <DOC_GET_METHOD>{12}</DOC_GET_METHOD>\n" +
            "    <BID_DOC_REFER_END_TIME>{13}</BID_DOC_REFER_END_TIME>\n" +
            "    <BID_DOC_REFER_METHOD>{14}</BID_DOC_REFER_METHOD>\n" +
            "    <BID_URL>{15}</BID_URL>\n" +
            "    <SYNDICATED_FLAG>{16}</SYNDICATED_FLAG>\n" +
            "    <IS_POSTPONE>{17}</IS_POSTPONE>\n" +
            "    <BID_OPEN_TIME>{18}</BID_OPEN_TIME>\n" +
            "    <BID_OPEN_METHOD>{19}</BID_OPEN_METHOD>\n" +
            "    <QUAL_TYPE>{20}</QUAL_TYPE>\n" +
            "    <TENDERERS>" +
            "    {21}"+
            "    \n\t</TENDERERS>\n" +
            "    <TENDER_AGENCYS>" +
            "    {22}"+
            "    \n\t</TENDER_AGENCYS>\n" +
            "    <OTHER_CONTENT>{23}</OTHER_CONTENT>\n" +
            "    <BULLETIN_DUTY>{24}</BULLETIN_DUTY>\n" +
            "    <EXAMINER_NAME>{25}</EXAMINER_NAME>\n" +
            "    <ATTACHMENT_SET> " +
            "      {26}"+
            "    \n\t</ATTACHMENT_SET>\n" +
            "    <TRADE_PLAT>{27}</TRADE_PLAT>\n" +
            "    <PUB_SERVICE_PLAT_CODE>{28}</PUB_SERVICE_PLAT_CODE>\n" +
            "    <SUBMIT_TIMESTAMP>{29}</SUBMIT_TIMESTAMP>\n" +
            "  </qualifyBulletinPhase>";


    public static StringBuilder BID_SECTION_CODES = new StringBuilder();

    public static String BID_SECTION_CODE ="\n\t\t<BID_SECTION_CODE>{0}" +
            "         </BID_SECTION_CODE>";

    public static StringBuilder TENDERERS = new StringBuilder();

    public static String TENDERER ="\n\t\t<TENDERER>\n" +
            "            <TENDER_CODE_TYPE>{0}</TENDER_CODE_TYPE>\n" +
            "            <TENDERER_CODE>{1}</TENDERER_CODE>\n" +
            "            <TENDERER_NAME>{2}</TENDERER_NAME>\n" +
            "            <TENDERER_ADDRESS>{3}</TENDERER_ADDRESS>\n" +
            "            <TENDERER_CONTACTOR>{4}</TENDERER_CONTACTOR>\n" +
            "            <TENDERER_PHONE_NUMBER>{5}</TENDERER_PHONE_NUMBER>\n" +
            "            <TENDERER_EMAIL>{6}</TENDERER_EMAIL>\n" +
            "    </TENDERER>\n";

    private static StringBuilder TENDER_AGENCYS = new StringBuilder();

    public static String TENDER_AGENCY = "\n\t\t<TENDER_AGENCY>\n" +
            "            <TENDER_AGENCY_CODE_TYPE>{0}</TENDER_AGENCY_CODE_TYPE>\n" +
            "            <TENDER_AGENCY_CODE>{1}</TENDER_AGENCY_CODE>\n" +
            "            <TENDER_AGENCY_NAME>{2}</TENDER_AGENCY_NAME>\n" +
            "            <TENDER_AGENCY_ADDRESS>{3}</TENDER_AGENCY_ADDRESS>\n" +
            "            <TENDER_AGENCY_CONTACTOR>{4}</TENDER_AGENCY_CONTACTOR>\n" +
            "            <TENDER_AGENCY_PHONE_NUMBER>{5}</TENDER_AGENCY_PHONE_NUMBER>\n" +
            "            <TENDER_AGENCY_EMAIL>{6}</TENDER_AGENCY_EMAIL>\n" +
            "    </TENDER_AGENCY>\n";


    private static StringBuilder ATTACHMENTS = new StringBuilder();

    public static String ATTACHMENT = "\n\t\t<ATTACHMENT>\n" +
            "            <ATTACHMENT_CODE>{0}</ATTACHMENT_CODE>\n" +
            "            <ATTACHMENT_FILE_NAME>{1}</ATTACHMENT_FILE_NAME>\n" +
            "            <FILE_TYPE>{2}</FILE_TYPE>\n" +
            "            <DATA_TIMESTAMP>{3}</DATA_TIMESTAMP>\n" +
            "            <MD5>{4}</MD5>\n" +
            "    \t</ATTACHMENT>";


    public static void setBidSectionCode(String bidSectionCode, List<String>  codes) {
        for (String code : codes){
            BID_SECTION_CODE = MessageFormat.format(bidSectionCode,code);
            BID_SECTION_CODES.append(BID_SECTION_CODE);
        }
    }

    public static void setTENDERER(String TENDERER, List<Map> lists) {
        if (lists == null){
            return;
        }
        for (Map<String,String> map : lists) {
            String args[] = new String[7];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            TENDERER = MessageFormat.format(TENDERER,args);
            TENDERERS.append(TENDERER);
        }
    }

    public static void setTenderAgency(String tenderAgency,List<Map> lists) {
        if (lists == null){
            return;
        }
        for (Map<String,String> map : lists) {
            String args[] = new String[7];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            TENDER_AGENCY = MessageFormat.format(TENDER_AGENCY,args);
            TENDER_AGENCYS.append(TENDER_AGENCY);
        }
    }

    public static void setATTACHMENT(String ATTACHMENT,List<Map> lists) {
        if (lists == null){
            return;
        }
        for (Map<String,String> map : lists) {
            String args[] = new String[5];
            int i=0;
            for(Map.Entry<String,String> entry : map.entrySet()){
                args[i++] = entry.getValue();
            }
            ATTACHMENT = MessageFormat.format(ATTACHMENT,args);
            ATTACHMENTS.append(ATTACHMENT);
        }
    }

    public static void setQualifyBulletinPhase( String qualifyBulletinPhase, String s0, String s1, String s2, String s3, String s4, String s5, String s6, String s7, String s8, String s9, String s10,
                                               String s11, String s12, String s13, String s14, String s15, String s16, String s17, String s18, String s19, String s20,
                                               String s21, String s22, String s23, String s24, String s25, String s26, String s27, String s28,String s29) {
        QualifyBulletinPhase.qualifyBulletinPhase = MessageFormat.format(qualifyBulletinPhase,s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,
                                                    s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,s26,s27,s28,s29);
    }


    public static void qualifyBulletinPhase(){
        setBidSectionCode(BID_SECTION_CODE,QualifyBulletinPhaseUtils.getStrings("BID_SECTION_CODES"));
        setTENDERER(TENDERER,QualifyBulletinPhaseUtils.getList(""));
        setTenderAgency(TENDER_AGENCY,QualifyBulletinPhaseUtils.getList(""));
        setATTACHMENT(ATTACHMENT,QualifyBulletinPhaseUtils.getList("ATTACHMENT_SET"));
        setQualifyBulletinPhase(QualifyBulletinPhase.qualifyBulletinPhase,QualifyBulletinPhaseUtils.get("TENDER_PROJECT_CODE"),
                                                                            BID_SECTION_CODES.toString(),
                                                                          QualifyBulletinPhaseUtils.get("TENDER_CODE"),
                                                                          QualifyBulletinPhaseUtils.get("NOTICE_NAME"),
                                                                          QualifyBulletinPhaseUtils.get("NOTICE_CONTENT"),
                                                                          QualifyBulletinPhaseUtils.get("NOTICE_SEND_TIME"),
                                                                          QualifyBulletinPhaseUtils.get("NOTICE_MEDIA"),
                                                                          QualifyBulletinPhaseUtils.get("URL"),
                                                                          QualifyBulletinPhaseUtils.get("NOTICE_NATURE"),
                                                                          QualifyBulletinPhaseUtils.get("NOTICE_TYPE"),
                                                                          QualifyBulletinPhaseUtils.get("DOC_GET_START_TIME"),
                                                                          QualifyBulletinPhaseUtils.get("DOC_GET_END_TIME"),
                                                                          QualifyBulletinPhaseUtils.get("DOC_GET_METHOD"),
                                                                          QualifyBulletinPhaseUtils.get("BID_DOC_REFER_END_TIME"),
                                                                          QualifyBulletinPhaseUtils.get("BID_DOC_REFER_METHOD"),
                                                                          QualifyBulletinPhaseUtils.get("BID_URL"),
                                                                          QualifyBulletinPhaseUtils.get("SYNDICATED_FLAG"),
                                                                          QualifyBulletinPhaseUtils.get("IS_POSTPONE"),
                                                                          QualifyBulletinPhaseUtils.get("BID_OPEN_TIME"),
                                                                          QualifyBulletinPhaseUtils.get("BID_OPEN_METHOD"),
                                                                          QualifyBulletinPhaseUtils.get("QUAL_TYPE"),
                                                                            TENDERERS.toString(),
                                                                            TENDER_AGENCYS.toString(),
                                                                          QualifyBulletinPhaseUtils.get("OTHER_CONTENT"),
                                                                          QualifyBulletinPhaseUtils.get("BULLETIN_DUTY"),
                                                                          QualifyBulletinPhaseUtils.get("EXAMINER_NAME"),
                                                                            ATTACHMENTS.toString(),
                                                                          QualifyBulletinPhaseUtils.get("TRADE_PLAT"),
                                                                          QualifyBulletinPhaseUtils.get("PUB_SERVICE_PLAT_CODE"),
                                                                          QualifyBulletinPhaseUtils.get("SUBMIT_TIMESTAMP")
                );
    }

    public static void main(String[] args) {

        qualifyBulletinPhase();
        System.out.println(qualifyBulletinPhase);
    }

}
